#region Namespace References

using System;
using System.Windows.Forms;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.WinForms.ControlBinders
{
    internal class SmartTextBoxBinder : SmartControlBinder<TextBox>
    {
        #region Public Instance Properties

        public override InputUpdateMode InputUpdate
        {
            get { return m_InputUpdate; }
            set
            {
                if (m_InputUpdate != value)
                {
                    m_InputUpdate = value;
                    UpdateInputUpdate();
                }
            }
        }

        public override HorizontalAlignment TextAlign
        {
            get { return m_TextAlign; }
            set
            {
                m_TextAlign = value;
                if (TypedBoundControl != null)
                {
                    TypedBoundControl.TextAlign = TextAlign;
                }
            }
        }

        #endregion

        #region Protected Instance Methods

        protected override void ReleaseBoundControl()
        {
            TypedBoundControl.TextChanged -= TypedBoundControl_TextChanged;
            TypedBoundControl.Leave -= TypedBoundControl_Leave;
        }

        protected override void SetDisplayValue(string value)
        {
            TypedBoundControl.Text = value;
        }

        protected override void UpdateBoundControl()
        {
            base.UpdateBoundControl();
            TypedBoundControl.TextAlign = TextAlign;
            UpdateInputUpdate();
        }

        protected override void UpdateBoundSmartField()
        {
            base.UpdateBoundSmartField();
            UpdateInputUpdate();
        }

        #endregion

        #region Private Instance Methods

        private void PostUserInput()
        {
            if ((TypedBoundControl != null) && (BoundSmartField != null))
            {
                string newValue = TypedBoundControl.Text;
                BoundSmartField.InputValue = newValue;
            }
        }

        private void TypedBoundControl_Leave(object sender, EventArgs e)
        {
            PostUserInput();
        }

        private void TypedBoundControl_TextChanged(object sender, EventArgs e)
        {
            PostUserInput();
        }

        private void UpdateInputUpdate()
        {
            if (BoundSmartField != null)
            {
                ReleaseBoundControl();

                TypedBoundControl.ReadOnly = BoundSmartField.InputInhibited.IsTrue;

                if (!TypedBoundControl.ReadOnly)
                {
                    switch (InputUpdate)
                    {
                        case InputUpdateMode.NoInput:
                            break;
                        case InputUpdateMode.CharacterEntryMode:
                            TypedBoundControl.TextChanged += TypedBoundControl_TextChanged;
                            break;
                        case InputUpdateMode.LeaveFieldMode:
                            TypedBoundControl.Leave += TypedBoundControl_Leave;
                            break;
                    }
                }
            }
        }

        #endregion

        #region Fields and Constants

        private InputUpdateMode m_InputUpdate = InputUpdateMode.LeaveFieldMode;
        private HorizontalAlignment m_TextAlign;

        #endregion
    }
}